﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WqLunTan.Areas.Manage.Models;
using WqLunTan.Filters;
using WqLunTan.Models.DataBase;

namespace WqLunTan.Areas.Manage.Controllers
{
    /// <summary>
    /// 查看微信推送的文本消息
    /// </summary>
    [Auth(AuthType = WqUserAuth.Manage)]
    public class WechatMessageController : BaseManageController
    {
        /// <summary>
        /// 查看微信消息列表
        /// </summary>
        /// <returns></returns>
        public IActionResult List(int pageIndex = 1, int pageSize = 20)
        {
            List<wechat_openid> list;
            var query = DbContext.WeChatOpenId.Where(x => x.ut_message > 0);
            var page = new PageModel() { CurrentPageIndex = pageIndex };

            page.TotalCount = query.Count();
            list = query.OrderBy(x => x.last_update).Skip(pageSize * pageIndex - pageSize).Take(pageSize).ToList();

            ViewData["list"] = list;
            ViewData["page"] = page;

            return View();
        }

        /// <summary>
        /// 查看消息记录详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult Detail(string id)
        {
            if (!string.IsNullOrWhiteSpace(id))
            {
                var wechatUser = DbContext.WeChatOpenId.Where(x => x.openid.Equals(id)).SingleOrDefault();
                if (wechatUser != null)
                {
                    var list = DbContext.WeChatData.Where(x => x.FromUserName.Equals(id)).OrderByDescending(x => x.CreateTime).Take(wechatUser.ut_message).ToList();
                    var msgIds = list.Select(x => x.MsgID).ToList();
                    var replyList = DbContext.WeChatReply.Where(x => x.openid.Equals(wechatUser.openid) && msgIds.Contains(x.MsgID)).ToList();

                    var startTime = DateTimeOffset.UtcNow.AddDays(-7);
                    var listHistory = DbContext.WeChatData.Where(x => x.FromUserName.Equals(id) && x.CreateTimeUtc >= startTime).OrderBy(x => x.CreateTime).ToList();
                    ViewData["list"] = list;
                    ViewData["listHistory"] = listHistory;
                    ViewData["replyList"] = replyList;
                    ViewData["wechatUser"] = wechatUser;
                }
            }

            return View();
        }

        /// <summary>
        /// 保存回复微信的消息
        /// </summary>
        /// <param name="reply"></param>
        /// <returns></returns>
        public IActionResult Save(wechat_reply reply)
        {
            if (!string.IsNullOrWhiteSpace(reply.content) && !string.IsNullOrWhiteSpace(reply.openid) && reply.MsgID > 0)
            {
                var model = DbContext.WeChatReply.Where(x => x.MsgID == reply.MsgID).SingleOrDefault();
                if (model != null)
                {
                    model.content = reply.content;
                    model.uid = LoginUser.Id;
                    model.last_update = DateTimeOffset.UtcNow;
                }
                else
                {
                    reply.uid = LoginUser.Id;
                    reply.is_send = false;
                    reply.create_time = DateTimeOffset.UtcNow;
                    reply.last_update = DateTimeOffset.UtcNow;
                    DbContext.WeChatReply.Add(reply);
                }
                DbContext.SaveChanges();
            }
            return JsonOkResult("保存成功");
        }
    }
}